public class Demo01 {
    private static final Object Lock = new Object();

    public static final int end = 100;
    public static volatile int start = 0; // 起始值改为1，因为要打印1-100

    public static final int THREAD_NUM = 3;

    public static class Print implements Runnable {
        public int index;

        public Print(int index) {
            this.index = index;
        }

        @Override
        public void run() {
            while (start <= end) {
                synchronized (Lock) {
                    while (start % THREAD_NUM != index && start <= end) {
                        try {
                            Lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    if (start <= end) {
                        System.out.println("THREAD:" + (index+1) + " 正在执行---------" + start);
                        start++;
                        Lock.notifyAll(); // 唤醒所有等待的线程
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        for (int i = 0; i <= THREAD_NUM; i++) {
            new Thread(new Print(i)).start();
        }
    }
}
